package kassiopeia.agent.seda.event;

import kassiopeia.agent.internal.metrics.MetricsAggregator;
import kassiopeia.plugin.Event;
import kassiopeia.plugin.Plugin;
import kassiopeia.plugin.ProcessorPlugin;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

@Configurable(dependencyCheck = true)
public class EventHandler implements Runnable {

    private Event event;
    private final Plugin plugin;
    private final Logger logger = Logger.getLogger(EventHandler.class);

    public EventHandler(Plugin plugin, Event event) {
        this.plugin = plugin;
        this.event = event;
    }

    @Override
    public void run() {
        try {
            ((ProcessorPlugin) plugin).process(event);
            if (MetricsAggregator.isEnabled()) {
                MetricsAggregator.countProcessedEvent(plugin);
            }
        } catch (Exception e) {
            logger.warn("Error in Processor Plugin [" + plugin.getContext().getPluginInstanceId() + "] :",e);
            if (MetricsAggregator.isEnabled()) {
                MetricsAggregator.countPluginError(plugin);
            }
        }
    }
}
